﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using WeatheRx.Data;

namespace WeatheRx.Managers
{
    public interface IWorkItemManager
    {
        WorkItem Get(int workItemId);

        void Store(WorkItem workItem);

        void StoreRange(List<WorkItem> workItems, bool deleteAllOthers = false);
    }

    public class WorkItemManager : IWorkItemManager
    {
        public WorkItem Get(int workItemId)
        {
            WorkItem result;
            using (var db = new HWAPEntities())
            {
                result = db.WorkItems.FirstOrDefault(x => x.WorkItemId == workItemId);
                if (result == null)
                    throw new ApplicationException("Work item was not found.");
            }
            return result;
        }

        public void Store(WorkItem workItem)
        {
            if (workItem.WorkItemId > 0)
                Update(workItem);
            else
                Add(workItem);
        }

        public void StoreRange(List<WorkItem> workItems, bool deleteAllOthers = false)
        {
            using (var db = new HWAPEntities())
            {
                var addList = db.WorkItems.Where(x => x.WorkItemId < 1).ToList();
                var updateList = db.WorkItems.Where(x => x.WorkItemId > 0).ToList();

                updateList.ForEach(x =>
                {
                    var firstOrDefault = db.WorkItems.FirstOrDefault(y => y.WorkItemId == x.WorkItemId);
                    if (firstOrDefault == null)
                    {
                        x.WorkItemId = 0;
                        addList.Add(x);
                    }
                    else
                    {
                        Mapper.Map(x, firstOrDefault);
                    }
                });

                db.WorkItems.AddRange(addList);
                if (deleteAllOthers)
                {
                    db.WorkItems.Where(x => workItems.All(y => !y.Equals(x)) && workItems.Any(y => y.WorkOrderId == x.WorkOrderId)).ToList().ForEach(x => x.Deleted = true);
                }
                db.SaveChanges();
            }
        }

        private void Add(WorkItem workItem)
        {
            using (var db = new HWAPEntities())
            {
                db.WorkItems.Add(workItem);
                db.SaveChanges();
            }
        }

        private void Update(WorkItem workItem)
        {
            using (var db = new HWAPEntities())
            {
                var firstOrDefault = db.WorkItems.FirstOrDefault(x => x.WorkItemId == workItem.WorkItemId);
                if (firstOrDefault == null)
                {
                    Add(workItem);
                    return;
                }

                Mapper.Map(workItem, firstOrDefault);
                db.SaveChanges();
            }
        }
    }
}